package com.wuyan.warriorsoffate.ui.view

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.LocalTextStyle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.lifecycle.MutableLiveData
import com.wuyan.warriorsoffate.ui.theme.WeTheme

private val REG_DEC_NUMBER=Regex("""\d+""")
private val REG_HEX_NUMBER=Regex("""[0-9a-fA-F]+""")
private fun isDecMax255(str:String):Boolean{
    if(REG_DEC_NUMBER.matchEntire(str)!=null&&str.length<4){
        val v=str.toInt()
        if (v in 0..255){
            return true
        }
    }
    return false
}
private fun isHexMax255(str:String):Boolean{
    if(REG_HEX_NUMBER.matchEntire(str)!=null&&str.length<3){
        val v=str.toInt(16)
        if (v in 0..255){
            return true
        }
    }
    return false
}
private fun isDecMax65535(str:String):Boolean{
    if(REG_DEC_NUMBER.matchEntire(str)!=null&&str.length<6){
        val v=str.toInt()
        if (v in 0..65535){
            return true
        }
    }
    return false
}
private fun isHexMax65535(str:String):Boolean{
    if(REG_HEX_NUMBER.matchEntire(str)!=null&&str.length<5){
        val v=str.toInt(16)
        if (v in 0..65535){
            return true
        }
    }
    return false
}
private fun isHexDec16777216(str:String):Boolean{
    if(REG_HEX_NUMBER.matchEntire(str)!=null&&str.length<9){
        val v=str.toInt()
        if (v in 0..0xffffff){
            return true
        }
    }
    return false
}
private fun isHexMax16777216(str:String):Boolean{
    if(REG_HEX_NUMBER.matchEntire(str)!=null&&str.length<7){
        val v=str.toInt(16)
        if (v in 0..0xffffff){
            return true
        }
    }
    return false
}
@Composable
fun TextFieldDec255(dec:Int,change:(value:Int)->Unit,modifier: Modifier = Modifier){
    TextFieldDecNumberBase(dec, {str->
        isDecMax255(str)
    },change,modifier.width(Dp(WeTheme.typography.body1.fontSize.value*2f)))
}
@Composable
fun TextFieldDec65535(dec:Int,change:(value:Int)->Unit,modifier: Modifier = Modifier){
    TextFieldDecNumberBase(dec, {str->
        isDecMax65535(str)
    },change,modifier.width(Dp(WeTheme.typography.body1.fontSize.value*10/3.0f)))
}
@Composable
fun TextFieldDec16777216(dec:Int,change:(value:Int)->Unit,modifier: Modifier = Modifier){
    TextFieldDecNumberBase(dec, {str->
        isHexDec16777216(str)
    },change,modifier.width(Dp(WeTheme.typography.body1.fontSize.value*14/3.0f)))
}
@Composable
fun TextFieldHex255(dec:Int,change:(value:Int)->Unit,modifier: Modifier = Modifier){
    TextFieldHexNumberBase(dec, {str->
        isHexMax255(str)

    }, change,modifier.width(Dp(WeTheme.typography.body1.fontSize.value*4/3.0f)))
}
@Composable
fun TextFieldHex65535(dec:Int,change:(value:Int)->Unit,modifier: Modifier = Modifier){
    TextFieldHexNumberBase(dec, {str->
        isHexMax65535(str)
    },change,modifier.width(Dp(WeTheme.typography.body1.fontSize.value*8/3.0f)))
}
@Composable
fun TextFieldHex16777216(dec:Int,change:(value:Int)->Unit,modifier: Modifier = Modifier){
    TextFieldHexNumberBase(dec, {str->
        isHexMax16777216(str)
    },change,modifier.width(Dp(WeTheme.typography.body1.fontSize.value*11/3.0f)))
}
@Composable
private fun TextFieldDecNumberBase(dec:Int,detection:(String)->Boolean,change:(value:Int)->Unit,modifier: Modifier = Modifier) {
    val str = remember {
        mutableStateOf(dec.toString())
    }
    TextFieldBase(if (dec==0&&str.value=="") "" else dec.toString(), modifier = modifier, onValueChange = { v ->
        if (v.isEmpty() || v == "") {
            str.value = ""
            change(0)
        } else if (detection(v)) {
            if (v[0]=='0'&&v.length>1){
                str.value = v.substring(1)
            }else{
                str.value = v
            }
            change(v.toInt())
        }
    }, keyboardOptions = KeyboardOptions(
        keyboardType= KeyboardType.Number
    ))
}
@Composable
private fun TextFieldHexNumberBase(dec:Int,detection:(String)->Boolean,change:(value:Int)->Unit,modifier: Modifier = Modifier) {
    val str = remember {
        mutableStateOf(dec.toString(16))
    }
    TextFieldBase(value = if (dec==0&&str.value=="") "" else dec.toString(16), modifier = modifier, onValueChange = { v ->
        if (v.isEmpty() || v == "") {
            str.value = ""
            change(0)
        } else if (detection(v)) {
            if (v[0]=='0'&&v.length>1){
                str.value = v.substring(1)
            }else{
                str.value = v
            }
            change(v.toInt(16))
        }else{
            str.value=str.value
        }
    })
}
@Composable
fun TextFieldBase(value: String,
                  onValueChange: (String) -> Unit,
                  modifier: Modifier = Modifier,
                  cursorBrush: Brush = SolidColor(WeTheme.colors.label2),
                  textStyle: TextStyle =WeTheme.typography.body1,
                  keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
                  keyboardActions: KeyboardActions = KeyboardActions.Default,
){
    val interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }
    val color= remember {
        mutableStateOf(Color(0))
    }
    val color0= remember {
        mutableStateOf(Color(0))
    }
    color0.value= WeTheme.colors.label2.copy(alpha = 0.5f)
    val color1= remember {
        mutableStateOf(Color(0))
    }
    color1.value= WeTheme.colors.bg.copy(alpha = 0.8f)
    BasicTextField(value, onValueChange,
        modifier.onFocusChanged {
            color.value=if(it.isFocused) color1.value  else color0.value
        },
        singleLine = true,
        interactionSource=interactionSource,
        cursorBrush=cursorBrush,
        textStyle=textStyle,
        keyboardOptions =keyboardOptions,
        keyboardActions = keyboardActions,
        decorationBox={innerTextField->
            Column() {
                innerTextField()
                Box(
                    Modifier
                        .padding(top = 4.dp)
                        .fillMaxWidth()
                        .height(1.dp)
                        .background(color.value))
            }
        },)
}

@Composable
fun TextFieldHexOrDex255(isHex: MutableState<Boolean>, dec: MutableState<Int>, modifier: Modifier = Modifier){
    if (isHex.value){
        TextFieldHex255(dec = dec.value, change = {
            dec.value=it
        },modifier=modifier)
    }else{
        TextFieldDec255(dec = dec.value, change = {
            dec.value=it
        },modifier=modifier)
    }
}
@Composable
fun TextFieldHexOrDex65535(isHex:MutableState<Boolean>,dec:MutableState<Int>,modifier: Modifier = Modifier){
    if (isHex.value){
        TextFieldHex65535(dec = dec.value, change = {
            dec.value=it
        },modifier=modifier)
    }else{
        TextFieldDec65535(dec = dec.value, change = {
            dec.value=it
        },modifier=modifier)
    }
}
@Composable
fun TextFieldHexOrDex16777216(isHex:MutableState<Boolean>,dec:Int,onChange: (dec:Int)->Unit,modifier: Modifier = Modifier){
    if (isHex.value){
        TextFieldHex16777216(dec = dec, change = {
            onChange(it)
        },modifier=modifier)
    }else{
        TextFieldDec16777216(dec = dec, change = {
            onChange(it)
        },modifier=modifier)
    }
}